<?php session_start(); ?>
<?php 
	include('misc/class_config.php'); 
	include_once('model/country.php');
?>

<?php 
	include('access_control/page_access.php');	
	
	setPreviousPage();
	declinePageIfLoggedIn();
	$currentUser = new user();
	$currentUser->isTimedout();
?>

<?php
	$unknownUser = new user();
	$registerMessage = '';
	$registerSuccess = false;
	$firstNameErrorMessage = '';
	$lastNameErrorMessage = '';
	$address1ErrorMessage = '';
	$address2ErrorMessage = '';
	$countryErrorMessage = '';
	$dobErrorMessage = '';
	$emailAddressErrorMessage = '';
	$passwordErrorMessage = '';
	
	if(isset($_POST['btnRegister']))
	{
		$registerValidationError = new validation();
		
		$firstNameErrorMessage = $registerValidationError->isEmpty(trim($_POST['first_name']),'<label class="required">First name is required.</label>');
		$lastNameErrorMessage = $registerValidationError->isEmpty(trim($_POST['last_name']),'<label class="required"> Last name required.</label>');
		$address1ErrorMessage = $registerValidationError->isEmpty(trim($_POST['address1']),'<label class="required">Address address is required.</label>');
		$address2ErrorMessage = $registerValidationError->isEmpty(trim($_POST['address2']),'<label class="required">Address address is required.</label>');
		$countryErrorMessage = $registerValidationError->isListItemSelected(trim($_POST['country']),'<label class="required">Select a Counrty.</label>');
		$dobErrorMessage = $registerValidationError->isEmpty(trim($_POST['dob']),'<label class="required">DOB is required.</label>');
		$emailAddressErrorMessage = $registerValidationError->isEmpty(trim($_POST['reg_email_address']),'<label class="required">Email address is required.</label>');
		if(empty($emailAddressErrorMessage))
		{
			$emailAddressErrorMessage = $registerValidationError->isEmailAddress(trim($_POST['reg_email_address']),'<label class="required">Invalid email address. Correct: Eg. johndoe@domain.com</label>');
		}
		$passwordErrorMessage = $registerValidationError->isEmpty($_POST['reg_password'],'<label class="required">Password address is required.</label>');
		
		$unknownUser->setUser('',$_POST['first_name'],$_POST['last_name'],$_POST['address1'],$_POST['address2'],$_POST['country'],$_POST['dob'],'user',$_POST['reg_password'],$_POST['reg_email_address'],'');
	}
?>

<?php
	
	if(isset($_POST['btnRegister']))
	{
		$formError = $registerValidationError->status;
		
		if($formError==0)
		{
			//$unknownUser = new user();
			try
			{
				//$unknownUser->setUser('',$_POST['first_name'],$_POST['last_name'],$_POST['address1'],$_POST['address2'],$_POST['country'],$_POST['dob'],'user',$_POST['password'],$_POST['email_address'],'');
				if($unknownUser->registerUser())
				{
					$registerSuccess=true;
					$registerMessage = '<span class="success_msg_span">You are now Registered!</span> Click <a href="login.php">here</a> to Login.';
				}
				else
				{
					$registerMessage = '<span class="failed_msg_span">An error occured while processing your registration. Please try again.</span>';
				}
			}
			catch(Exception $ex)
			{
				$registerMessage = $ex->getMessage();
			}
		}
	}
?>

<?php
	if(!$currentUser->isLoggedIn())
	{
		include('controller/loginformcontroller.php'); 
	}
?>

<?php $pageTitle = "Register | Everything"; ?>
<?php include('layout/page_start.php'); ?>
<?php include('layout/page_header.php'); ?>
<?php
	if(!$currentUser->loggedIn)
	{
		include('layout/loginform.php'); 
	}
?>
<?php include('layout/menu_bar.php'); ?>

<div id="main_content_div"></br>
	<div class="page_message_div"><p><?php echo $registerMessage; ?></p></div>
	<?php
		if(!$registerSuccess)
		{
			include_once "layout/registerform.php";
		}
		else
		{
			echo '<div class="page_action_msg_div"><a>'.$registerMessage.'</a></div>';
		}
	?>
</div>
<?php include('layout/page_footer.php'); ?>
<?php include('layout/page_end.php'); ?>
<?php setCurrentPage();?>